package com.cn.jdia.di.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBManager4Mysql {

	public static final String DRIVER = "com.mysql.jdbc.Driver";
	public static final String USER = "dcenter";
	public static final String PASS = "Qd83789951";
	public static final String URL = "jdbc:mysql://172.20.5.179:3306/datacenter?useUnicode=true&characterEncoding=utf-8";

	private static DBManager4Mysql per = null;
	private Connection conn = null;
	private Statement stmt = null;

	private DBManager4Mysql() {

	}

	public static DBManager4Mysql createInstance() {
		if (per == null) {
			per = new DBManager4Mysql();
			per.initDB();
		}
		return per;
	}

	public void initDB() {
		try {
			Class.forName(DRIVER);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void connectDB() {
		try {
			conn = DriverManager.getConnection(URL, USER, PASS);
			stmt = conn.createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// System.out.println("SqlManager:Connect to database successful...");
	}

	public void closeDB() {
		try {
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// System.out.println("Close connection successful");
	}

	public ResultSet executeQuery(String sql) {
		ResultSet rs = null;
		try {
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}

	public int executeUpdate(String sql) {
		int ret = 0;
		try {
			ret = stmt.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ret;
	}

}
